#include "CEvent_Manager.h"

CEventManager* CEventManager::GetInstance()
{
	static CEventManager instance;
	return &instance;
}
void CEventManager::RegisterEvent(string EventID, IEvent* ClassToRegister)
{
	// check for already registered events.
	for(int i = 0; i < m_RegisteredEvents.size(); i++)
	{
		if(m_RegisteredEvents[i].sEvent == EventID)
		{
			for(int j = 0; j < m_RegisteredEvents[i].RegisteredClasses.size() ; j++)
			{
				//if class is already registered to the event, then don't do shit.
				if(m_RegisteredEvents[i].RegisteredClasses[j] == ClassToRegister)
				{
					return;
				}

			}

			m_RegisteredEvents[i].RegisteredClasses.push_back(ClassToRegister);
			return;
		}

	}


	Event NewEvent;
	NewEvent.sEvent = EventID;
	NewEvent.RegisteredClasses.push_back(ClassToRegister);

	m_RegisteredEvents.push_back(NewEvent);
}
void CEventManager::UnregisterEvent(string EventID, IEvent* ClassToUnregister)
{
	for(int i = 0; i < m_RegisteredEvents.size(); i++)
	{
		if(m_RegisteredEvents[i].sEvent == EventID)
		{
			for(int j = 0; j < m_RegisteredEvents[i].RegisteredClasses.size() ; j++)
			{
				//if class is already registered to the event, then don't do shit.
				if(m_RegisteredEvents[i].RegisteredClasses[j] == ClassToUnregister)
				{
					m_RegisteredEvents[i].RegisteredClasses.erase(m_RegisteredEvents[i].RegisteredClasses.begin() + j);
					return;
				}

			}
			
		}

	}

}
void CEventManager::DropEvent(string EventID)
{
	for(int i = 0; i < m_RegisteredEvents.size(); i++)
	{
		if(m_RegisteredEvents[i].sEvent == EventID)
		{
			m_RegisteredEvents.erase(m_RegisteredEvents.begin() + i);
		}
	}

}
void CEventManager::FireEvent(string EventID)
{
	m_EventsToProcess.push_back(EventID);
}
void CEventManager::ClearAllEvents()
{
	m_RegisteredEvents.clear();
}
void CEventManager::ProcessEvents()
{
	for(int i = 0; i < m_EventsToProcess.size() ; i++)
	{
		for(int j = 0; j < m_RegisteredEvents.size(); j++)
		{
			if(m_RegisteredEvents[j].sEvent == m_EventsToProcess[i])
			{
				for(int k = 0; k < m_RegisteredEvents[j].RegisteredClasses.size(); k++)
				{
					m_RegisteredEvents[j].RegisteredClasses[k]->HandleEvent(m_EventsToProcess[i]);
				}
			}
		}
	}

	m_EventsToProcess.clear();
}